package com.jy.adscm.task.job;

import com.jy.adscm.bean.JwdEntity;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.declarationForm.service.JyFormTermManageService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.mapper.JyHospitalBaseMapper;
import com.jy.adscm.task.bean.JyJwdTask;
import com.jy.adscm.task.mapper.JyJwdTaskMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务
 */
@Component
public class JyDealerManageJob {

    private  Logger logger = Logger.getLogger(getClass());


    @Autowired
    JyJwdTaskMapper jyJwdTaskMapper;
    @Autowired
    JyHospitalBaseMapper jyHospitalBaseMapper;
    @Autowired
    DealerMapper dealerMapper;
    @Autowired
    JyFormTermManageService jyFormTermManageService;


    /**
     * 更新报单的过程记录期限任务（每天晚上22点执行）
     * @throws AdscmSystemException
     */
    @Scheduled(cron = "0 0 22 * * ? ")
    protected void updateFormTermTimeJob() throws AdscmSystemException{
        logger.info("更新报单的过程记录期限任务---start");

        try{
            jyFormTermManageService.updateFormTermTime();
        }catch (Exception e){
            logger.error("更新报单的过程记录期限任务异常",e);
        }

        logger.info("更新报单的过程记录期限任务---end");
    }


    /**
     * 更新经销商、医院的地址经纬度的任务
     * @throws Exception
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    protected void updateJwdJob() throws AdscmSystemException{
        logger.info("更新经销商、医院的地址经纬度的任务---start");

        List<Integer> successTaskIds = new ArrayList<>();


        List<JyJwdTask> tasks = jyJwdTaskMapper.selectList(null);

        if (tasks != null && tasks.size()>0){
            for (JyJwdTask task : tasks) {
                try{
                    Thread.sleep(200);
                    if (task.getTableType() == 1){
                        //表示是医院信息
                        JyHospitalBase hospital = jyHospitalBaseMapper.selectById(task.getTableId());
                        if(hospital != null){

                            String address = hospital.getAddressSheng() + hospital.getAddressShi() + hospital.getAddressQuxian() + hospital.getAddressDetail();

                            JwdEntity jwd = AdscmBaseUtils.getInstance().getJwdByAddress(address);

                            if (jwd != null){
                                hospital.setHospitalJd(jwd.getJd());
                                hospital.setHospitalWd(jwd.getWd());
                                jyHospitalBaseMapper.updateById(hospital);
                                successTaskIds.add(task.getId());
                            }
                        }
                    }else {
                        //表示是经销商信息
                        Dealer dealer = dealerMapper.selectById(task.getTableId());

                        String address = dealer.getProvince() +dealer.getCity() + dealer.getZone() + dealer.getAddress();

                        JwdEntity jwd = AdscmBaseUtils.getInstance().getJwdByAddress(address);

                        if (jwd != null){
                            dealer.setLongitude(jwd.getJd());
                            dealer.setLatitude(jwd.getWd());

                            dealerMapper.updateById(dealer);

                            successTaskIds.add(task.getId());
                        }

                    }


                }catch (Exception e){
                    logger.error("",e);
                }


            }

            if (successTaskIds.size()>0){
                jyJwdTaskMapper.deleteBatchIds(successTaskIds);
            }


        }



        logger.info("更新经销商、医院的地址经纬度的任务---end");
    }
}
